﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;

namespace BitcoinExchange.Wallet
{
    public class KorbitWallet
    {
        MainInfo _info;

        public double KRWAmount;
        public double USDAmount;
        public double BTCAmount;

        public KorbitWallet(MainInfo info)
        {
            _info = info;
            KRWAmount = 0.0;
            USDAmount = 0.0;
            BTCAmount = 0.0;
        }

        public void RetrievedWallet()
        {
            if (!_info.IsLogin())
                return;

            string strData;
            string strURL;
            string strParameter;

            Dictionary<string, string> dicURLParameter = _info.GetKorbitURLParameterAccessKey();

            strURL = _info.GetKorbitURL() + StringURL.URL_KORBIT_WALLET;
            strParameter = _info.ConvertURLParameter(dicURLParameter);

            strURL = strURL + "?" + strParameter;
            strParameter = "";

            strData = _info.RequestData(strURL, "");

            if (strData.Length == 0)
                return;

            ConvertToData(strData);
        }

        private void ConvertToData(string strData)
        {

            JObject objData;

            if (strData.Length == 0)
                return;

            objData = JObject.Parse(strData);

            // 계좌 정보를 추출
            JArray arrObj = (JArray)objData["in"];

            //계좌 잔고 정보를 추출
            arrObj = (JArray)objData["balance"];

            foreach (JToken item in arrObj)
            {
                if (item["currency"].ToString() == "krw")
                {
                    KRWAmount = Double.Parse(item["value"].ToString());
                }
                else if (item["currency"].ToString() == "btc")
                {
                    BTCAmount = Double.Parse(item["value"].ToString());
                }
            }
        }
    }
}
